<?php
namespace app\controller\api\info;


use app\BaseController;
use app\common\repositories\info\MapTaskRecordRepository;
use app\common\repositories\info\MapTaskRepository as repository;
use basic\services\LocationService;
use think\App;
use think\exception\ValidateException;

class MapTask extends BaseController
{

    protected repository $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }


    public function lst()
    {
        [$page,$limit] = $this->request->page();
        $where = [
            'uid'=>request()->uid()
        ];
        return success($this->repository->getApiLst($where,$page,$limit));
    }


    public function record()
    {
        [$page,$limit] = $this->request->page();
        $where = $this->request->params([
           'type','tags','filter','city'
        ]);
        $where['uid'] = request()->uid();
        return success(app()->make(MapTaskRecordRepository::class)->getList($where,$page,$limit));
    }

    public function recordDelete($id)
    {
        $user = app()->make(MapTaskRecordRepository::class);
    }

    public function location2Address()
    {
        $rs = LocationService::create()->location2Address(input('lat'),input('lng'));
        return success(['data'=>$rs]);
    }

    public function recordDetail($id)
    {
        $detail = app()->make(MapTaskRecordRepository::class)->get($id);
        return success($detail);
    }


    public function recordCopy($id)
    {
        $detail = app()->make(MapTaskRecordRepository::class)->get($id);
        if($detail){
            $detail->save(['is_copy'=>1]);
        }
        return success('复制成功');
    }


    public function recordFilterData()
    {
        $citys = app()->make(MapTaskRecordRepository::class)->getSearch()->where(['uid'=>request()->uid()])->group('city')->column('city');
        $tags = app()->make(MapTaskRecordRepository::class)->getSearch()->where('keyword','<>','')->where(['uid'=>request()->uid()])->group('keyword')->column('keyword');

        return success(compact('citys','tags'));
    }



    public function getData()
    {
        $search = $this->request->params(
            ['keywords',['city',''],'count','search_type','map_type',['filter',0],'lng','lat',['range',10]]
        );

        if(!$search['keywords']){
            throw new ValidateException('请输入关键字');
        }

        if(!$search['count']){
            throw new ValidateException('请选择数量');
        }

        if(!in_array($search['map_type'],[1,2,3])){
            $search['map_type'] = mt_rand(1,3);
        }

        $search['search_type'] =  $search['search_type'] == 2 ? 2 : 1;

        if($search['search_type'] == 2 && (!$search['lat'] || !$search['lng'])){
            throw new ValidateException('请授权定位');
        }

//        $search = [
//            'keywords'=>'理发店',
//            'city'=>'温州',
//            'count'=>50,
//            'search_type'=>1, //搜索类型 1全城 2周边
//            'map_type'=>1,
//            'filter'=>0,
//            'lng'=>'120.717228',
//            'lat'=>'27.988200',
//            'range'=>10
//        ];

        return success($this->repository->runTask($search));

    }


}